package org.uim.cricket.storage.raise;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.uim.cricket.db.impl.ExistCountry;
import org.uim.cricket.db.impl.FetchAllContinents;
import org.uim.cricket.storage.Dlogger;
import org.uim.cricket.storage.gui.AskCountryDetails;

public class CheckForCountry {

	private static List<String> continents = null;
	
	static {
		FetchAllContinents fc = new FetchAllContinents();
		try {
			fc.fetch();
			Map<String, Integer> conts = fc.getMap();
			continents = new ArrayList<String>(conts.keySet());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public String raiseCountry(String country) throws Exception {
		if (country == null || country.equalsIgnoreCase("null") || country.length() == 0) return "";
		ExistCountry exist = new ExistCountry(country);
		boolean isexist = exist.exists();
		if (!isexist) {
			Dlogger.debug("  [New Country]: " + country );
			
			AskCountryDetails cdetails = new AskCountryDetails(null, true, country, continents);
			cdetails.setVisible(true);
			String cnt = cdetails.getContinent();
			String abbr = cdetails.getAbbreviation();
			boolean assoc = cdetails.isAssociated();
			
			String res = new InsertNewCountry(null, country, cnt, abbr, assoc).raise();
			cdetails.dispose();
			
			return res;
		}
		return "";
	}
	
}
